package com.lance.answer.question_export;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lance.answer.dto.QuestionAnswer;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author yixun
 * @version 2.0.0
 * @description  整合所有题目
 * @date 2021-06-08 19:41
 * @since 2.0.0
 */
public class QuestionJoin {

    public static void main(String[] args) throws IOException {
      //  join("03427");
     //   join("01628");
      //  join("03129");
       // join("02480");
       // join("医学免疫学与微生物学");
     //   join("病理学与病理生理学");
    //    join("经济数学基础12");
     //   join("建筑材料(A)");
      //  join("阅读与写作(1)");
    //    join("毛泽东思想和中国特色社会主义理论体系概论");
    //    join("人体解剖学与组织胚胎学");
      //  join("人体生理学");
     //   join("医护心理学");
       // join("医学生物化学");
     //   join("建筑工程项目招投标与合同管理");
   //     join("Dreamweaver网页设计");
     //   join("政府经济学");
       // join("学前儿童科学教育");
     //   join("西方经济学（本）");
     //   join("组织行为学");
    //    join("公司概论");
       // join("劳动与社会保障法");
     //   join("形势与政策");
     //   join("思想道德与法治");
      //  join("中国现代文学专题");
        join("西方行政学说");
    }

    public static void join(String subjectName) throws IOException {
        Set<String> set = new HashSet<>();
        List<QuestionAnswer>  list = new ArrayList<>();
        File file = new File("src/main/resources/" + subjectName + "/questions/");
        Collection<File> files = FileUtils.listFiles(file,null,false);
        System.out.println(files.size());
        System.out.println("-------------用户名-----------，------题目数量------");
        for (File file1:files) {
            String fileName = file1.getName();
            String str1 = FileUtils.readFileToString(file1,"utf-8");
            List<QuestionAnswer> list1 = JSONObject.parseArray(str1,QuestionAnswer.class);
            String userName = fileName.replace(".json","");
            System.out.println(userName + "," + list1.size());
            for (QuestionAnswer questionAnswer:list1) {
                if (set.contains(questionAnswer.getQuestionCode())) {
                    continue;
                } else {
                    set.add(questionAnswer.getQuestionCode());
                    list.add(questionAnswer);
                }
            }
        }
        Collections.sort(list);
        System.out.println("去重聚合题目数量：" + list.size());
        File file1 = new File("src/main/resources/" + subjectName + "/" + subjectName + "-answer.json");
        FileUtils.write(file1,JSONObject.toJSONString(list, SerializerFeature.PrettyFormat),"utf-8");
    }
}
